package com.zmn.oms.zmn.business.impl.work.list;

import com.google.common.collect.Lists;
import com.zmn.brs.common.enums.EventEnum;
import com.zmn.brs.common.enums.RecordFieldEnum;
import com.zmn.brs.starter.annotation.RecordField;
import com.zmn.brs.starter.annotation.StaffOperateRecord;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.oms.business.interfaces.work.OrderWorkListBService;
import com.zmn.oms.common.constant.OrderConsts;
import com.zmn.oms.common.constant.OrderStatusConsts;
import com.zmn.oms.model.entity.work.OrderBaseEsQuery;
import com.zmn.oms.model.entity.work.OrderWorkQuery;
import com.zmn.oms.model.entity.work.list.OrderWorkBranchFinanceCountQuery;
import com.zmn.base.oms.common.model.es.orderwork.EsOrderWork;
import com.zmn.oms.model.vo.work.list.ZsOrderWorkBranchFinanceCountVO;
import com.zmn.oms.model.vo.work.list.ZsOrderWorkBranchFinanceVO;
import com.zmn.oms.zmn.business.interfaces.work.ZsOrderWorkQueryBService;
import com.zmn.oms.zmn.business.interfaces.work.list.ZsOrderWorkBranchFinanceQueryBService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 类描述：子公司财务列表查询
 *
 * @author liuying
 * @date 2018/12/27 12:02
 */
@Service
public class ZsOrderWorkBranchFinanceQueryBServiceImpl implements ZsOrderWorkBranchFinanceQueryBService {

    @Autowired
    ZsOrderWorkQueryBService zsOrderWorkQueryBService;
    @Autowired
    private OrderWorkListBService orderWorkListBService;

    @Override
    public ZsOrderWorkBranchFinanceCountVO findWorkCount(OrderBaseEsQuery query) {
        ZsOrderWorkBranchFinanceCountVO vo = new ZsOrderWorkBranchFinanceCountVO();

        // 查询待算账的工单 - 工单状态为 已收单
        OrderWorkBranchFinanceCountQuery waitAccountCountQuery = BeanMapper.map(query, OrderWorkBranchFinanceCountQuery.class);
        waitAccountCountQuery.setWorkStatus(OrderStatusConsts.WORK_STATUS_CHECKOUT);
        vo.setWaitAccountCount(orderWorkListBService.countOrderWorkByQuery(waitAccountCountQuery));

        // 查询工单配件报销状态为：待报销的工单
        OrderWorkBranchFinanceCountQuery waitPartReimburseCountQuery = BeanMapper.map(query, OrderWorkBranchFinanceCountQuery.class);
        waitPartReimburseCountQuery.setPartReimburseStatus(OrderConsts.PART_REIMBURSE_STATUS_DOING);
        vo.setWaitPartReimburseCount(orderWorkListBService.countOrderWorkByQuery(waitAccountCountQuery));

        // 查询工单配件报销状态为：待报销的工单
        OrderWorkBranchFinanceCountQuery invoicedCountQuery = BeanMapper.map(query, OrderWorkBranchFinanceCountQuery.class);
        // 获取本月时间
        invoicedCountQuery.setInvoiceTime(String.format("%s,%s", DateUtil.getNowFormatted("yyyy-MM-01 00:00:00"), DateUtil.getNowFormatted("yyyy-MM-dd 23:59:59")));
        vo.setInvoicedCount(orderWorkListBService.countOrderWorkByQuery(waitAccountCountQuery));
        return vo;
    }

    @Override
    public Integer findMasterCountByQuery(OrderWorkQuery query) {
        return orderWorkListBService.countMasterByQuery(query);
    }

    @StaffOperateRecord(staffId = "{{#query.staffId}}", event = EventEnum.WORK_ORDER_QUERY,
            mapping = {
            @RecordField(field = RecordFieldEnum.STAFF_IP, value = "{{#query.ip}}"),
            @RecordField(field = RecordFieldEnum.PLAT, value = "{{#query.platWork}}")
    })
    @Override
    public List<ZsOrderWorkBranchFinanceVO> listPageOrderWorkByQuery(OrderWorkQuery query) {
        return this.listPageOrderWorkByQueryNoRecord(query);
    }

    @Override
    public List<ZsOrderWorkBranchFinanceVO> listPageOrderWorkByQueryNoRecord(OrderWorkQuery query) {
        zsOrderWorkQueryBService.processWorkQuery(query);
        // 查询
        List<EsOrderWork> esOrderWorks = orderWorkListBService.listPageOrderWorkByQuery(query);

        // 转 vo
        List<ZsOrderWorkBranchFinanceVO> voList = Lists.newArrayList();
        esOrderWorks.forEach((esOrderWork -> {
            ZsOrderWorkBranchFinanceVO vo = BeanMapper.map(esOrderWork, ZsOrderWorkBranchFinanceVO.class);
            vo.setWorkId(esOrderWork.getId());
            voList.add(vo);
        }));
        return voList;
    }

    @Override
    public String exportListOrderWorkByQuery(OrderWorkQuery query) {
        return null;
    }
}
